/*
 * Serum
 *
 * @license      GNU Lesser General Public Licence see LICENCE-LGPL file or http://www.gnu.org/licenses/lgpl.html
 */

var items;
if (logged == true) {
	items = [{
		xtype :         'combo',
        name :          'fieldType',
        hiddenName:     'fieldType',
        displayField :  'name',
        fieldLabel :    'Type',
        allowBlank:     false,
        editable:       false,
        forceSelection: true,
        mode:           'local',
        valueField:     'value',
        value:          'serum',
        triggerAction:  'all',
        store:          new Ext.data.SimpleStore({
                            fields: ['value', 'name'],
                            data: [['serum',    'Serum'     ],
                                   ['project',  'Project'   ]]
                        })
    }, {
        xtype :         'combo',
        name :          'displayedfieldAdapter',
        hiddenName:     'fieldUpdate',
        displayField :  'name',
        fieldLabel :    'MAJ vers',
        allowBlank:     false,
        editable:       false,
        forceSelection: true,
        mode:           'local',
        valueField:     'value',
        value:          'latest',
        triggerAction:  'all',
        store: new Ext.data.SimpleStore({
            fields: ['value', 'name'],
            data: [['latest',    'Dernière version'     ],
                   ['version',   'Choisir la version'    ]]
        })
    }, {
        name :          'fieldVersion',
        fieldLabel :    'Version',
        value:          '',
        allowBlank:     true
    }];
} else {
	items = [{
        name :          'fieldLogin',
        fieldLabel :    'Identifiant',
        value:          '',
        allowBlank:     false
    },  {
        name :          'fieldPwd',
        fieldLabel :    'Mot de passe',
        value:          '',
        allowBlank:     false
    }, {
		xtype :         'combo',
        name :          'fieldType',
        hiddenName:     'fieldType',
        displayField :  'name',
        fieldLabel :    'Type',
        allowBlank:     false,
        editable:       false,
        forceSelection: true,
        mode:           'local',
        valueField:     'value',
        value:          'serum',
        triggerAction:  'all',
        store:          new Ext.data.SimpleStore({
                            fields: ['value', 'name'],
                            data: [['serum',    'Serum'     ],
                                   ['project',  'Project'   ]]
                        })
    }, {
        xtype :         'combo',
        name :          'displayedfieldAdapter',
        hiddenName:     'fieldUpdate',
        displayField :  'name',
        fieldLabel :    'MAJ vers',
        allowBlank:     false,
        editable:       false,
        forceSelection: true,
        mode:           'local',
        valueField:     'value',
        value:          'latest',
        triggerAction:  'all',
        store: new Ext.data.SimpleStore({
            fields: ['value', 'name'],
            data: [['latest',    'Dernière version'     ],
                   ['version',   'Choisir la version'    ]]
        })
    }, {
        name :          'fieldVersion',
        fieldLabel :    'Version',
        value:          '',
        allowBlank:     true
    }];
}

Ext.onReady(function() {
    Ext.QuickTips.init();

    var updateForm = new Ext.FormPanel({
        url:            'validation.php',
        frame:          true,
        defaultType:    'textfield',
        monitorValid:   true,

        title :         'Mise à jour de Skalpel Serum',
        items :        	items,
        buttons :
            [{
                text:           'Valider',
                formBind:       true,

                handler: function() {
            		updateForm.getForm().submit({
                        method:    'POST',
                        waitTitle: 'Validation',
                        waitMsg:   'Envoi des données en cours...',

                        success: function(form, action) {
                             Ext.MessageBox.show({
                                title:      'Mise à jour en cours...',
                                msg:        'Veuillez patienter pendant la mise à jour de Skalpel Serum...',
                                width:      250,
                                wait:       true,
                                waitConfig: { interval: 100 }
                            });
                            obj = Ext.util.JSON.decode(action.response.responseText);
                            window.location = obj.page;
                        },

                        failure: function(form, action) {
                            if (action.failureType == 'server') {
                                obj = Ext.util.JSON.decode(action.response.responseText);
                                if (typeof obj.errors.reason !== 'undefined') {
                                    Ext.Msg.show({
                                       title: 'Erreur de connexion',
                                       msg: '<br />' + obj.errors.reason,
                                       modal: true,
                                       icon: Ext.MessageBox.ERROR,
                                       buttons: Ext.MessageBox.OK
                                    });
                                } else {
                                    for(var error in obj.errors.fields) {
                                        var field = this.find('name', error)[0];
                                        if (typeof field !== 'undefined' && field instanceof Ext.form.Field) {
                                            field.markInvalid(obj.errors.fields[error]);
                                        }
                                    }
                                }
                            } else {
                                Ext.Msg.alert('Warning!', 'Authentication server is unreachable : ' + action.response.responseText);
                            }
                        },
                        scope: updateForm
                    });
                },
                scope: updateForm
            }]
    })


    var win = new Ext.Window({
            closable:   false,
            resizable:  false,
            border:     false,
            items:      updateForm
        });
    win.show();

});

    